import 'package:provider/provider.dart';
import 'package:flutter/material.dart';
import '../core/auth_store.dart';
import '../core/theme_config.dart';
import 'package:url_launcher/url_launcher.dart';
import '../utils/util.dart';
import 'auth_page.dart';
class AccountPage extends StatefulWidget {
  const AccountPage({Key? key}) : super(key: key);
  @override
  _AccountPageState createState() => _AccountPageState();
}
class _AccountPageState extends State<AccountPage> {
  @override
  Widget build(BuildContext context) {
     final themeConfig = Provider.of<ThemeConfig>(context);
    final auth = Provider.of<AuthStore>(context);
    return Scaffold(
      appBar: AppBar(
        title: const Text('个人中心'),
        toolbarHeight: 32,
        centerTitle:  false,
        backgroundColor: themeConfig.currentGradient.colors.first,
        automaticallyImplyLeading: false,
        //字体颜色
        titleTextStyle: TextStyle(fontSize: 18,color: themeConfig.currentTextColor),
        ),

      body: Container(
        decoration: BoxDecoration(gradient: themeConfig.currentGradient),
        child: Center(
          child: ConstrainedBox(
            constraints: const BoxConstraints(maxWidth: 560),
            child: Padding(
              padding: const EdgeInsets.all(16.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  if (!auth.loggedIn)
                    Container(
                      padding: const EdgeInsets.all(16),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(16),
                        boxShadow: [
                          BoxShadow(color: Colors.black.withOpacity(0.06), blurRadius: 16, spreadRadius: 2),
                        ],
                      ),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          const Icon(Icons.person_outline, size: 64, color: Colors.black54),
                          const SizedBox(height: 12),
                          Text('尚未登录', style: TextStyle(color: themeConfig.currentTextColor, fontSize: 18, fontWeight: FontWeight.bold)),
                          const SizedBox(height: 8),
                          Text('登录以同步观看记录与设置', style: TextStyle(color: themeConfig.currentTextColor.withOpacity(0.7))),
                          const SizedBox(height: 16),
                          SizedBox(
                            height: 44,
                            child: ElevatedButton(
                              onPressed: () {
                                Navigator.of(context).push(MaterialPageRoute(builder: (_) => const AuthPage()));
                              },
                              style: ElevatedButton.styleFrom(backgroundColor: themeConfig.currentPrimaryColor),
                              child: const Text('登录/注册'),
                            ),
                          ),
                        ],
                      ),
                    )
                  else
                    Container(
                      padding: const EdgeInsets.all(16),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(16),
                        boxShadow: [
                          BoxShadow(color: Colors.black.withOpacity(0.06), blurRadius: 16, spreadRadius: 2),
                        ],
                      ),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.stretch,
                        children: [
                          ListTile(
                            leading: CircleAvatar(
                              radius: 24,
                              backgroundColor: themeConfig.currentPrimaryColor.withOpacity(0.85),
                              child: Text(
                                (auth.username ?? '').isNotEmpty ? (auth.username![0]).toUpperCase() : 'U',
                                style: const TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
                              ),
                            ),
                            title: Text('用户名：${auth.username ?? ''}', style: TextStyle(color: themeConfig.currentTextColor, fontWeight: FontWeight.w600)),
                            subtitle: Text('手机号：${_maskPhone(auth.phone)}', style: TextStyle(color: themeConfig.currentTextColor.withOpacity(0.7))),
                          ),
                          const SizedBox(height: 12),
                          Row(
                            children: [
                              Expanded(
                                child: OutlinedButton(
                                  onPressed: () => CommonUtils.checkAndPromptUpdate(context, manual: true),
                                  child: const Text('版本检测'),
                                ),
                              ),
                              const SizedBox(width: 12),
                              Expanded(
                                child: ElevatedButton(
                                  onPressed: () => auth.logout(),
                                  style: ElevatedButton.styleFrom(backgroundColor: Colors.redAccent),
                                  child: const Text('退出登录'),
                                ),
                              ),
                            ],
                          ),
                        ],
                      ),
                    ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
  
  String _maskPhone(String? s) {
    if (s == null || s.length < 7) return s ?? '';
    return s.replaceRange(3, s.length - 4, '*' * (s.length - 7));
  }
}
